#pragma once

#include "comm/my_buf.h"
#include "comm/my_str.h"

// read a line of data in reentrant state machine.
typedef struct {
    int                     state;
    my_str_t                line;
} my_http_readline_sm_t;

static inline void
my_http_readline_sm_reset(my_http_readline_sm_t *sm)
{
    bzero(sm, sizeof(my_http_readline_sm_t));
}

my_result_t my_http_readline(my_http_readline_sm_t *readline_sm, my_buf_t *buf);

my_bool_t my_http_empty_line(my_http_readline_sm_t *readline_sm);



typedef enum {
    SW_CHUNK_PRESTART = 0,
    SW_CHUNK_START,
    SW_CHUNK_SIZE,
    SW_CHUNK_DATA,
    SW_CHUNK_AFTER_DATA_CRCF
} my_http_chunk_state_t;


typedef struct {
    int64_t                 size;
    int64_t                 remain;
    my_http_chunk_state_t   state;
    /* chunk-ext-name */
    /* chunk-ext-val */
} my_http_chunk_sm_t;

static inline void
my_http_chunk_sm_reset(my_http_chunk_sm_t *sm)
{
    sm->size = 0;
    sm->remain = 0;
    sm->state = SW_CHUNK_PRESTART;
}

my_result_t my_http_chunk_size_parse(my_http_chunk_sm_t *sm, my_str_t *line);